package com.sunny.example.string;

import java.util.*;

/**
 * 查找兄弟单词
 */
public class StringHJ27 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        try {
            while (scanner.hasNext()) {
                String input = scanner.nextLine();
                String[] strs = input.split(" ");
                if (strs == null || strs.length < 3) {
                    continue;
                }
                //兄弟单词个数
                int wordsN = Integer.parseInt(strs[0]);
                int targetWordN = Integer.parseInt(strs[strs.length - 1]);
                String targetWord = strs[strs.length - 2];
                String[] dicStrs = new String[wordsN];
                System.arraycopy(strs, 1, dicStrs, 0, wordsN);

                List<String> rightList = new ArrayList<>();
                for (int i = 0; i < dicStrs.length; i++) {
                    String word = dicStrs[i];
                    if (word.equals(targetWord) || word.length() != targetWord.length()) {
                        continue;
                    }
                    //开始验证此字符串是否为兄弟单词
                    checkWords(word, targetWord, rightList);
                }

                Collections.sort(rightList);

                System.out.println(rightList.size());
                if (rightList.size() > 0) {
                    System.out.println(rightList.get(targetWordN > 0 ? targetWordN - 1 : 0));
                }


            }
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    /**
     * 校验单词
     *
     * @param word
     * @param targetWord
     * @param rightList
     */
    private static void checkWords(String word, String targetWord, List<String> rightList) {
        char[] srcChars = word.toCharArray();
        char[] destChars = targetWord.toCharArray();

        Arrays.sort(srcChars);
        Arrays.sort(destChars);

        if (String.valueOf(srcChars).equals(String.valueOf(destChars))) {
            rightList.add(word);
        }

    }
}
